# -*- python -*-

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
from SCons.Util import Split, CLVar

env = Environment()

# Keep ebuild up to date with appropriate headers, or else figure
# out how to get scons to handle header installation as well.
sources = env.Split("""
                    adjustment_method.cc
                    adjustment_method_2.cc
                    assembly_program.cc
                    third_party/bsdiff_apply.cc
                    third_party/bsdiff_create.cc
                    crc.cc
                    difference_estimator.cc
                    disassembler.cc
                    disassembler_elf_32_x86.cc
                    disassembler_win32_x86.cc
                    encoded_program.cc
                    ensemble.cc
                    ensemble_apply.cc
                    ensemble_create.cc
                    memory_allocator.cc
                    simple_delta.cc
                    streams.cc
                    """)

BASE_VER = os.environ.get('BASE_VER', '125070')

env.Append(
    CPPPATH=['files'],
    CCFLAGS=['-g']
)
for key in Split('CC CXX AR RANLIB LD NM'):
  value = os.environ.get(key)
  if value:
    env[key] = Split(value)
env['PKG_CONFIG'] = os.environ.get('PKG_CONFIG', 'pkg-config')

# Get random compiler flags.
for key in Split('CFLAGS CXXFLAGS'):
  value = os.environ.get(key)
  if value:
    env[key] += CLVar(os.environ[key])

var_map = {
  "CPPFLAGS" : "CCFLAGS",
  "LDFLAGS"  : "LINKFLAGS",
}
for env_key in var_map.keys():
  if os.environ.has_key(env_key):
    scons_key = var_map[env_key]
    env[scons_key] += CLVar(os.environ[env_key])

env['CCFLAGS'] += ['-fPIC',
                   '-fno-exceptions',
                   '-DCOURGETTE_USE_CRC_LIB',
                   '-I..']

env.ParseConfig('%s --cflags --libs libchrome-%s' % (env['PKG_CONFIG'],
                                                     BASE_VER))

# Fix issue with scons not passing some vars through the environment.
for key in Split('PKG_CONFIG_LIBDIR PKG_CONFIG_PATH SYSROOT'):
  if os.environ.has_key(key):
    env['ENV'][key] = os.environ[key]

env.StaticLibrary('courgette', sources)
